package com.jsgt.greekfire.ui.coin.drawers

import android.app.Dialog
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.marginLeft
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.CountDownTimerUtils
import kotlinx.android.synthetic.main.activity_binding_payment_setting.*
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.textColor


class BindingPaymentSettingActivity : BaseActivity() {
    private val type: String by lazy { intent.getStringExtra("type") }      // wechat 微信    支付宝 alipay
    private val name: String by lazy { intent.getStringExtra("name") }
    private val account: String by lazy { intent.getStringExtra("account") }
    private val ivcode: Int by lazy { intent.getIntExtra("ivcode", 0) }
    private val unlink: Boolean by lazy { intent.getBooleanExtra("unlink", false) }   //是否解绑
    var title_tv: TextView? = null
    var right_tv: TextView? = null

    var ivDialogClosed: ImageView? = null
    var tvDlReceivables: TextView? = null
    var tvDlPayment: TextView? = null
    var tvReceivablesPayment: TextView? = null

    var tvAffirm: TextView? = null
    var tvCancel: TextView? = null

    override fun getLayoutId(): Int = R.layout.activity_binding_payment_setting
    override fun main() {
        title_tv = findViewById(R.id.title_tv)
        right_tv = findViewById(R.id.right_tv)
        if (!TextUtils.isEmpty(type)) {
            if (type.equals("wechat")) {
                initWechatView()
            } else if (type.equals("alipay")) {
                initAlipayView()
            }
        }
        if (unlink) {
            right_tv?.text = resources.getString(R.string.binding_payment_unlink)
            right_tv?.textColor = resources.getColor(R.color.color_388DEE)
            right_tv?.visibility = View.VISIBLE
            llSendCode?.visibility = View.GONE
            btnSave?.visibility = View.GONE
            etName?.setText(name)
            etAccount?.setText(account)
            if (ivcode != 0)
                ivUpload?.setImageResource(ivcode)
        } else {
            right_tv?.visibility = View.GONE
            llSendCode?.visibility = View.VISIBLE
            btnSave?.visibility = View.VISIBLE
            ivUpload?.visibility = View.GONE
        }
        tvSendCode.setOnClickListener { v ->
            val mCountDownTimerUtils = CountDownTimerUtils(tvSendCode, 5000, 1000,1)
            mCountDownTimerUtils.start()

        }
        rllReceivablesMode.setOnClickListener { v ->
            showPaymentDialog()
        }
        right_tv?.setOnClickListener { v ->
            showUnlinkDialog();
        }
    }

    /**
     * 解绑
     */
    private fun showUnlinkDialog() {
        val button1Dialog: Dialog = Dialog(this, R.style.DialogTheme)
        val view: View = LayoutInflater.from(this).inflate(R.layout.dialog_unlink, null)
        button1Dialog.setContentView(view)
        button1Dialog.window!!.setGravity(Gravity.CENTER)
        button1Dialog.show()
        tvAffirm = view.findViewById(R.id.tvAffirm)
        tvCancel = view.findViewById(R.id.tvCancel)
        tvAffirm?.setOnClickListener { v ->
            ToastUtils.show("确定解绑")
            button1Dialog.dismiss()
        }
        tvCancel?.setOnClickListener { v ->
            button1Dialog.dismiss()
        }
    }
    /**
     * 选择展示设置
     */
    private fun showPaymentDialog() {
        val button1Dialog: Dialog = Dialog(this, R.style.DialogTheme)
        val view: View = LayoutInflater.from(this).inflate(R.layout.dialog_payment, null)
        button1Dialog.setContentView(view)
        var layoutParams: ViewGroup.LayoutParams = view.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels
        view.layoutParams = layoutParams
        button1Dialog.window!!.setGravity(Gravity.BOTTOM)
        button1Dialog.window!!.setWindowAnimations(R.style.main_menu_animStyle)
        button1Dialog.show()

        ivDialogClosed = view.findViewById(R.id.ivDialogClosed)
        tvDlReceivables = view.findViewById(R.id.tvDlReceivables)
        tvDlPayment = view.findViewById(R.id.tvDlPayment)
        tvReceivablesPayment = view.findViewById(R.id.tvReceivablesPayment)

        ivDialogClosed?.setOnClickListener { v ->
            button1Dialog.dismiss()
        }
        tvDlReceivables?.setOnClickListener { v ->
            tvModalities?.text = "用于收款"
            button1Dialog.dismiss()
        }
        tvDlPayment?.setOnClickListener { v ->
            tvModalities?.text = "用于付款"
            button1Dialog.dismiss()
        }
        tvReceivablesPayment?.setOnClickListener { v ->
            tvModalities?.text = "用于收付款"
            button1Dialog.dismiss()
        }

    }

    private fun initWechatView() {
        title_tv?.text = resources.getString(R.string.binding_payment_text1)
        tvName?.text = resources.getString(R.string.binding_payment_text3)
        etName?.hint = resources.getString(R.string.binding_payment_text11)
        tvAccount?.text = resources.getString(R.string.binding_payment_text4)
        etAccount?.hint = resources.getString(R.string.binding_payment_text12)
        tvCollectionCode?.text = resources.getString(R.string.binding_payment_text7)
        tvUpload?.text = resources.getString(R.string.binding_payment_text8)

    }

    private fun initAlipayView() {
        title_tv?.text = resources.getString(R.string.set_alipay)
        tvName?.text = resources.getString(R.string.binding_payment_text14)
        etName?.hint = resources.getString(R.string.binding_payment_text15)
        tvAccount?.text = resources.getString(R.string.binding_payment_text17)
        etAccount?.hint = resources.getString(R.string.et_alipay_account)
        tvCollectionCode?.text = resources.getString(R.string.binding_payment_text17)
        tvUpload?.text = resources.getString(R.string.binding_payment_text18)
    }
}